package utilities.manager;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JLabel;

import utilities.exceptions.JFrameNameUsedException;

public final class JFrameManager 
{
	public static final Dimension STND_DEFAULT = new Dimension(400,300);
	public static final Dimension HD_DEFAULT = new Dimension(1600,900);
	
	private static HashMap<String, JFrame> JFRAMES = new HashMap<String, JFrame>();
	private static int jframeCount = 0;
	
	private JFrameManager() {}
	
	public static JFrame makeWindow(String windowName, Dimension windowSize, Color backgroundColor, boolean isVisible) throws JFrameNameUsedException
	{
		JFrame frame = null;
		
		// How big should the window be? What should be the window's background color?
		Dimension size = windowSize == null ? STND_DEFAULT : windowSize;
		Color bgColor = backgroundColor == null ? Color.white : backgroundColor;
		String wName = windowName;
		
		// What is the window's name? (If none is specified...)
		if(windowName == null || windowName.isEmpty() || windowName.replaceAll("\\s", "").equals(""))
			wName = "WINDOW_" + jframeCount;
		
		if(JFRAMES.containsKey(wName))
		{
			System.out.println("Cannot use name " + wName + "; this name is already in use!");
			throw new JFrameNameUsedException(wName);
		}
		
		// JFrame is always at the highest level; it is basically the window itself. Everything else is put into the JFrame.
		frame = new JFrame(wName);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(size);
		
		// Object that will force JFrame to be the specified size with specified background color
		JLabel filler = new JLabel();
		filler.setOpaque(true);
		filler.setPreferredSize(size);
		filler.setBackground(bgColor);

		// Put in filler object; should the window be visible?
		frame.getContentPane().add(filler, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(isVisible);
		
		// Keep track of this window!
		JFRAMES.put(wName, frame);
		jframeCount++;
		
		return frame;
	}

	// Get one of the JFrames that are stored by the JFrameManager
	public static JFrame getGameWindow(String windowName)
	{
		return JFRAMES.get(windowName);
	}
}
